package scau.erps.view.business.team;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import scau.erps.app.Erps;
import scau.erps.entity.Comprehensivefee;
import scau.erps.entity.Sand;
import scau.erps.service.ComprehensivefeeService;
import scau.erps.service.SandService;
import scau.erps.service.impl.ComprehensivefeeServiceImpl;
import scau.erps.service.impl.SandServiceImpl;


/**
 * 综合费用表
 * @author FYJ
 *
 */
public class MyCostPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	protected JComboBox<String> teamCombo;
	protected JButton searchBtn, operationBtn1, operationBtn2, operationBtn3,
			operationBtn4;
	protected JPanel contentPanel, actionPanel;
	protected JScrollPane dataPanel;
	protected JTable table;
	protected DefaultTableCellRenderer render;
	private OrderTableModel dataModel;

	public MyCostPanel() {
		searchBtn = new JButton("显示综合费用");
		searchBtn.addActionListener(this);
		actionPanel = new JPanel(); // 顶部交互操作面板
		actionPanel.add(searchBtn);
		table = new JTable();
		table.setPreferredScrollableViewportSize(new Dimension(930, 500));
		table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		dataPanel = new JScrollPane(table);
		dataPanel.setViewportView(table);
		render = new DefaultTableCellRenderer();
		render.setHorizontalAlignment(JLabel.CENTER);
		table.setDefaultRenderer(Object.class, render);
		contentPanel = new JPanel();
		contentPanel.setLayout(new BorderLayout());
		contentPanel.add(actionPanel, BorderLayout.NORTH);
		contentPanel.add(dataPanel, BorderLayout.CENTER);
		this.add(contentPanel);
	}

	private OrderTableModel getOrderTableModel() {
		dataModel = new OrderTableModel();
		return dataModel;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchBtn) {//显示综合费用表
			dataModel = getOrderTableModel();
			this.table.setModel(dataModel);
		}
	}

}

class OrderTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;
	private String[] title = { "费用", "数目" };
	private Object[][] data = null;
	final int team_Id = Erps.getCurrentTeam().getId();
	SandService sandService = Erps.getIocInstance().get(SandServiceImpl.class);
	Sand sand = sandService.getSandByTeam(team_Id);
	char year=sand.getTime().charAt(1);
	String y;
	
	public OrderTableModel() {
		if(year!='0')
		y = "Y"+(char)(sand.getTime().charAt(1)-1);
		else
		y="Y0";
		ComprehensivefeeService comprehensivefeeService = Erps
				.getIocInstance()
				.get(ComprehensivefeeServiceImpl.class);
					
			List<Comprehensivefee> list = comprehensivefeeService
				.getComprehensivefee(team_Id,y);
			Comprehensivefee comprehensivefee = list.get(0);
		data = new Object[10][title.length];
	    data[0][0]= "行政管理费";
	    data[0][1]= comprehensivefee.getManagementFee();
	    data[1][0]="广告费";
	    data[1][1]= comprehensivefee.getAdvertisingFee();
	    data[2][0]="设备维护费";
	    data[2][1]= comprehensivefee.getMaintenanceFee(); 
	    data[3][0]="设备改造费";
	    data[3][1]= comprehensivefee.getSwitchFee();
	    data[4][0]="租金";
	    data[4][1]= comprehensivefee.getRent();
	    data[5][0]="产品研发";
	    data[5][1]= comprehensivefee.getResearch();
	    data[6][0]="市场开拓";
	    data[6][1]= comprehensivefee.getMarketFee();
	    data[7][0]="ISO认证";
	    data[7][1]= comprehensivefee.getIso();
	    data[8][0]= "其他";
	    data[8][1]= comprehensivefee.getOther();
	    data[9][0]="合计";
	    data[9][1]=comprehensivefee.getTotal();//读取综合费用表信息
	}

	@Override
	public int getRowCount() {
		return this.data.length; // 行数
	}

	@Override
	public int getColumnCount() {
		return this.title.length; // 列数
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data[rowIndex][columnIndex]; // 取表格单元格值
	}

	public String getColumnName(int columnIndex) {
		return title[columnIndex]; // 显示标题
	}

}
